package cn.bin758.h5game.module.game.service.users;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.bin758.h5game.module.game.controller.admin.users.vo.*;
import cn.bin758.h5game.module.game.dal.dataobject.users.GameUsersDO;
import cn.bin758.h5game.framework.common.pojo.PageResult;

import cn.bin758.h5game.module.game.convert.users.GameUsersConvert;
import cn.bin758.h5game.module.game.dal.mysql.users.GameUsersMapper;

import static cn.bin758.h5game.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.bin758.h5game.module.game.enums.ErrorCodeConstants.*;

/**
 * 玩家信息 Service 实现类
 *
 * @author bin758
 */
@Service
@Validated
public class GameUsersServiceImpl implements GameUsersService {

    @Resource
    private GameUsersMapper usersMapper;

    @Override
    public Long createUsers(GameUsersCreateReqVO createReqVO) {
        // 插入
        GameUsersDO users = GameUsersConvert.INSTANCE.convert(createReqVO);
        usersMapper.insert(users);
        // 返回
        return users.getId();
    }

    @Override
    public void updateUsers(GameUsersUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateUsersExists(updateReqVO.getId());
        // 更新
        GameUsersDO updateObj = GameUsersConvert.INSTANCE.convert(updateReqVO);
        usersMapper.updateById(updateObj);
    }

    @Override
    public void deleteUsers(Long id) {
        // 校验存在
        this.validateUsersExists(id);
        // 删除
        usersMapper.deleteById(id);
    }

    private void validateUsersExists(Long id) {
        if (usersMapper.selectById(id) == null) {
            throw exception(USERS_NOT_EXISTS);
        }
    }

    @Override
    public GameUsersDO getUsers(Long id) {
        return usersMapper.selectById(id);
    }

    @Override
    public List<GameUsersDO> getUsersList(Collection<Long> ids) {
        return usersMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<GameUsersDO> getUsersPage(GameUsersPageReqVO pageReqVO) {
        return usersMapper.selectPage(pageReqVO);
    }

    @Override
    public List<GameUsersDO> getUsersList(GameUsersExportReqVO exportReqVO) {
        return usersMapper.selectList(exportReqVO);
    }

}
